package com.yeyks.common.utils;

import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;

import java.util.Objects;

public class PinyinUtils {


    /**
     * 中文转拼音
     * @param chinese 中文
     * @return 返回结果
     */
    public static String getPinyin(String chinese){

        try {
            return PinyinHelper.convertToPinyinString(chinese,"", PinyinFormat.WITHOUT_TONE);
        } catch (PinyinException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 中文转大写字母
     * @param chinese 中文
     * @return 返回结果
     */
    public static String getPinyinUpperCase(String chinese){

        return Objects.requireNonNull(PinyinUtils.getPinyin(chinese)).toUpperCase();
    }

    public static void main(String[] args) {
        String city = "杭州市";

        try {
            System.out.println(PinyinHelper.convertToPinyinString(city,"", PinyinFormat.WITHOUT_TONE));

            System.out.println(PinyinHelper.getShortPinyin(city));
        } catch (PinyinException e) {
            e.printStackTrace();
        }
    }
}
